<?php
include '../../libraries/dbconfig.php';
//unset($_SESSION['display']);
session_start();
$productid = $_REQUEST['productid'];
$qty = $_REQUEST['qty'];
$discount = $_REQUEST['discount'];

# Find out if the form had been submitted
if (isset($productid) && isset($qty) && isset($discount)) {

    if ($_SESSION['display']) {

        $display = $_SESSION['display'];
        array_push($display, array("productid" => $productid, "qty" => $qty, "discount" => $discount));
        $_SESSION['display'] = $display;
    } else {

        $display = array();
        //$display[0] = $_POST;

        $display[0] = array("productid" => $productid, "qty" => $qty, "discount" => $discount);

        $_SESSION['display'] = $display;
    }
//$total_vat=$_POST['vat'];
}
?>
<form action="something.php" method="post">
    <div style="clear:both"></div>
    <!-------------------------------------------Group selection options----------------->
    <div class="group_select_desk" style="clear:both">
        <span><strong style="font-size:18px;">Billing Items</strong>
            <!---  <a onclick="javascript:checkAll('frmMain', false);" href="javascript:void();" style="float:right">uncheck all</a>
              <a onclick="javascript:checkAll('frmMain', true);" href="javascript:void();" style="float:right; margin-right: 20px;">check all</a></span>-->
            <!--<a href="javascript:reserveCheck('chkDel[]')" style="float:right">Invert Select</a>
            <a href="javascript:allCheck('chkDel[]',false)" style="float:right; margin-right: 20px;">Select None</a> 
            <a href="javascript:allCheck('chkDel[]',true)" style="float:right; margin-right: 20px;">Select All</a>--></span>


        <button class="btn" type="submit" name="delete" style="float:right;margin-right: 10px" onclick="javascript: form.action = '';"><i class="icon-trash"></i>Delete</button>
        <div class="btn-group"  style="float: right;margin-right: 10px">
            <a class="btn dropdown-toggle" data-toggle="dropdown" href="#" >
                Check&nbsp;
                <span class="caret"></span>
            </a>
            <ul class="dropdown-menu"  style="float:left;margin-left: -84px">
                <!-- dropdown menu links -->
                <li><a href="javascript:allCheck('chkDel[]',true)">Select All</a></li>
                <li><a href="javascript:allCheck('chkDel[]',false)">Select None</a></li> 
                <li><a href="javascript:reserveCheck('chkDel[]')" >Invert Select</a></li>
            </ul>
        </div>
        <button class="btn btn-success" type="submit" name="save" onclick="javascript: form.action = '../../libraries/save_sales_billing';" style="float:right;margin-right:10px">Save Bill</button>
        <a href ="add_stock.php" style="float:right;margin-right: 10px"><button class="btn" type="button"><i class="icon-file"></i>New</button></a>
    </div>

    <div class="group_select_mob" style="clear:both;">
        <span style="width:100%;"><strong style="font-size:22px;">Billing List</strong></span><br><br>

        <div class="btn-group"  style="float: left">
            <a class="btn dropdown-toggle" data-toggle="dropdown" href="#" >
                Select&nbsp;
                <span class="caret"></span>
            </a>
            <ul class="dropdown-menu"  style="float:left;margin-left: -84px">
                <!-- dropdown menu links -->
                <li><a href="javascript:allCheck('chkDel[]',true)">Select All</a></li>
                <li><a href="javascript:allCheck('chkDel[]',false)">Select None</a></li> 
                <li><a href="javascript:reserveCheck('chkDel[]')" >Invert Select</a></li>   
            </ul>
        </div>
        <div class="btn-group"  style="float: right">
            <a class="btn dropdown-toggle" data-toggle="dropdown" href="#" >
                Action&nbsp;
                <span class="caret"></span>
            </a>
            <ul class="dropdown-menu"  style="float:left;margin-left: -84px">
                <!-- dropdown menu links -->
                <li><a href="add_sales_billing.php"><i class="icon-file"></i>New</a></li> 
                <li>

                    <button class="btn btn-success" type="submit" name="save" onclick="javascript: form.action = '../../libraries/save_sales_billing';" style="width:100%">Save Bill</button></li>
        </div>
    </div>

    <br />


    <!------------------------------Details Table------------------------------------->
    <table  class="table1 table-striped" style="margin-left: auto;margin-right: auto;margin-top: 15px; ">
        <thead  class="thead1">
            <tr>
                <th></th>
                <th>SL NO</th>
                <th>Product Name</th>
                <th>Rate</th>
                <th>QTY</th>
                <th>TAX</th>
                <th>Discount</th>
                <th>Amount</th>
                <th>Edit</th>
            </tr>
        </thead>
        <tbody>
<?php
$result = $display;
$dsc = mysql_query("select * from options");
$dsc_data = mysql_fetch_array($dsc);
for ($row = 0; $row < count($result); $row++) {
    // echo "  Name :" . $result[$row]['productid'] . " <br> qty :" . $result[$row]['qty'];
    // echo "<br />";
    $id = 0;
    $product_id = $result[$row]['productid'];

    $products = mysql_query("select * from products where productid = $product_id");

    $products_data = mysql_fetch_array($products);
    if ($dsc_data['discount_type'] == "amount") {
        $discount_product = $result[$row]['discount'] * $result[$row]['qty'];
        $price = ($products_data['price'] - $result[$row]['discount']) * $result[$row]['qty'];
        $vat = ($price / 100) * 14.5;
        $amount = $price + $vat;
    } else {
        $discount = ($products_data['price'] / 100) * $result[$row]['discount'];
        $discount_product = (($products_data['price'] / 100) * $result[$row]['discount']) * $result[$row]['qty'];
        $price = ($products_data['price'] - $discount) * $result[$row]['qty'];
        $vat = ($price / 100) * 14.5;
        $amount = $price + $vat;
    }
    ?>


                <!-------------------------Dynamic row to display from database--------------------->
                <tr>
                    <td><input type="checkbox" name="chkDel[]" id="chkDel<?= $i; ?>" value="<?php echo $row; ?>"></td>
                    <td><? echo $id; ?></td>
                    <td><a href="stock_register_update.php?id=<?= $result[$row]['productid']; ?>"><?= $products_data['name']; ?></a></td>
                    <td><?= $products_data['price']; ?></td>
                    <td><?= $result[$row]['qty'] ?></td>
                    <td><?= $vat; ?></td>
                    <td><?= $discount_product ?></td>
                    <td><?= $amount; ?></td>
                    <td><a href="stock_register_update.php?id=<?= $stock_data['rowid']; ?>">Edit</a></td>
                </tr>

    <?php
    // Closing the loop
    $id++;
}
?>
        </tbody>
    </table>

    <!----------------------------CRUD Buttons-------------------------------------->
    <!--<div align="center" style="margin-top:10px">
        <button class="btn btn-danger" type="submit" name="save" onclick="javascript: form.action='../../libraries/save_sales_billing';">Save Bill</button>
    </div>-->
</form>